<?php
namespace app\index\controller;

use think\Controller;
use think\Db;
class BaseInfo extends Common{

    //基础信息类页面渲染
    public function index(){


        return $this->fetch();
    }
    //添加页面
    public function add(){
        //获取基础信息
        $info = Db('company')
            ->select();
        $this->assign('company', $info);

        return $this->fetch();
    }
    //编辑页面
    public function edit(){
        $id = request()->param('id');


        //获取基础信息
        $info = Db('company')
            ->select();
        $this->assign('company', $info);

        $info = Db('user')
        ->alias("user")
        ->join('base_info', 'user.id = base_info.user_id')
        ->join('company','base_info.company_id = company.id')
        ->where('user.id','=',$id)
        ->find();
        
        $this->assign('info', $info);
        return $this->fetch();
    }
    //基础信息表格数据渲染
    public function getBaseinfo(){
        $page = request()->param('page');
        $limit = request()->param('limit');
        $start = ($page-1) * $limit;
        $modules = input("modules");
        $value =input("value");


        
        if($value && $modules){
            $info = Db('user')
            ->alias("user")
            ->join('base_info', 'user.id = base_info.user_id')
            ->join('company', 'base_info.company_id = company.id')
            ->where($modules,'LIKE','%'.$value .'%')
            ->where('base_info.isdelete',0)
            ->limit($start,$limit)
            ->select();
            
            $count =  Db('user')
            ->limit($start,$limit)
            ->count();




        }else{
            $info = Db('user')
            ->alias("user")
            ->join('base_info', 'user.id = base_info.user_id')
            ->join('company', 'base_info.company_id = company.id')
            ->where('base_info.isdelete',0)
            ->limit($start,$limit)
            ->select();
            
            $count =  Db('user')
            ->limit($start,$limit)
            ->count();
        }


        

        $msg = [
            'code' => 0,
            'msg' => '查询成功',
            'data' => $info,
            'count' => $count
        ];
        
        echo json_encode($msg);
    }
    //用户页面添加信息处理
    public function addPost(){
        $id = Db('user')->order('id desc')->limit(1)->find()['id'] +1;
        $status=Db('user')
        ->insert([
            "id" => $id,
            "name" => request()->param("name"), 
            "job_number" => request()->param("job_number"), 
            "password" => md5(request()->param("password")), 
            "role_id" => 1,  
        ]);
        $status1=Db('base_info')
        ->insert([
            "user_id" => $id,
            "sex" =>request()->param("sex"),
            "company_id" =>request()->param("company_id"),
            "birthday" =>request()->param("birthday"),
            "working_hours" =>request()->param("working_hours"),
            "highest_education" =>request()->param("highest_education"),
            "title" =>request()->param("title"),
            "job_category" =>request()->param("job_category"),
            "current_administrative_position" =>request()->param("current_administrative_position"),
            "current_administrative_position_level" =>request()->param("current_administrative_position_level"),
            "tenure" =>request()->param("tenure"),
            "part_time_administrative_position" =>request()->param("part_time_administrative_position"),
            "part_time_administrative_position_level" =>request()->param("part_time_administrative_position_level"),
            "part_time_company" =>request()->param("part_time_company"),
            "part_time_tenure" =>request()->param("part_time_tenure"),
            "previous_administrative_position" =>request()->param("previous_administrative_position"),
            "previous_administrative_position_level" =>request()->param("previous_administrative_position_level"),
            "previous_tenure" =>request()->param("previous_tenure"),
            "idcard_number" =>request()->param("idcard_number"),
            "contact_information" =>request()->param("contact_information"),
            "bank_card_number" =>request()->param("bank_card_number"),
            "highest_academic_title" =>request()->param("highest_academic_title"),
            "starting_salary_time_of_the_current_year" =>request()->param("starting_salary_time_of_the_current_year"),
            "stoping_salary_time_of_the_current_year" =>request()->param("stoping_salary_time_of_the_current_year")
        ]);
        $status2=Db('filesalary_info')
        ->insert([
            "user_id" => $id,
        ]);
        $status3=Db('realsalary_info')
        ->insert([
            "user_id" => $id,
        ]);

        if($status){
            if($status1){
                if($status2){
                    if($status3){
                        $this->success('成功！！！');
                    }else{
                        $this->success('插入realsalary_info表失败！！！');
                    }
                }else{
                    $this->success('插入filesalary_info表失败！！！');
                }
            }else{
                $this->success('插入base_info表失败！！！');
            }
        }else{
            $this->success('插入user表失败！！！');
        }

        
    }
    //用户页面编辑信息处理
    public function editPost(){
        $id = request()->param('id');

        $status=Db('user')
        ->alias("user")
        ->join('base_info', 'user.id = base_info.user_id')
        ->where('user.id','=',$id)
        ->update([
            "user.name" => request()->param("name"),
            "base_info.sex" => request()->param("sex"),
            "base_info.company_id" => request()->param("company_id"),
            "base_info.birthday" => request()->param("birthday"),
            "base_info.working_hours" => request()->param("working_hours"),
            "base_info.highest_education" => request()->param("highest_education"),
            "base_info.title" => request()->param("title"),
            "base_info.job_category" => request()->param("job_category"),
            "base_info.current_administrative_position" => request()->param("current_administrative_position"),
            "base_info.current_administrative_position_level" => request()->param("current_administrative_position_level"),
            "base_info.tenure" => request()->param("tenure"),
            "base_info.part_time_administrative_position" => request()->param("part_time_administrative_position"),
            "base_info.part_time_administrative_position_level" => request()->param("part_time_administrative_position_level"),
            "base_info.part_time_company" => request()->param("part_time_company"),
            "base_info.part_time_tenure" => request()->param("part_time_tenure"),
            "base_info.previous_administrative_position" => request()->param("previous_administrative_position"),
            "base_info.previous_administrative_position_level" => request()->param("previous_administrative_position_level"),
            "base_info.previous_tenure" => request()->param("previous_tenure"),
            "base_info.idcard_number" => request()->param("idcard_number"),
            "base_info.contact_information" => request()->param("contact_information"),
            "base_info.bank_card_number" => request()->param("bank_card_number"),
            "base_info.highest_academic_title" => request()->param("highest_academic_title"),
            "base_info.starting_salary_time_of_the_current_year" => request()->param("starting_salary_time_of_the_current_year"),
            "base_info.stoping_salary_time_of_the_current_year" => request()->param("stoping_salary_time_of_the_current_year")
        ]);
        if($status){
            $this->success('更新成功');
        }else{
            $this->error('更新失败');
        }
    }
    //用户页面删除信息处理
    public function remove(){
        if(request()->param('id')){
            $id     = request()->param('id'); //获取id
            $status = Db('user')
                     ->alias("user")
                    ->where('user.id',$id)
                    ->join('base_info', 'user.id = base_info.user_id')
                    ->join('filesalary_info', 'user.id = filesalary_info.user_id')
                    ->join('realsalary_info', 'user.id = realsalary_info.user_id')
                    ->update([
                        'user.isdelete' =>"1",
                        'base_info.isdelete' =>"1",
                        'filesalary_info.isdelete' =>"1",
                        'realsalary_info.isdelete' =>"1",
                    ]);
            if($status){
                $this->success('删除成功');
            }else{
                $this->error('删除失败');
                exit;
            }
        }
    }
    //用户页面删除选中信息
    public function removeselected(){
        //接收参数  
        $map = input();
        
        $IDs=implode(',', $map['id']);
        $where = 'user.id in('.$IDs.')';  

        $list=Db('user')
            ->alias("user")
            ->where($where)
            ->join('base_info', 'user.id = base_info.user_id')
            ->join('filesalary_info', 'user.id = filesalary_info.user_id')
            ->join('realsalary_info', 'user.id = realsalary_info.user_id')
            ->update([
                'user.isdelete' =>"1",
                'base_info.isdelete' =>"1",
                'filesalary_info.isdelete' =>"1",
                'realsalary_info.isdelete' =>"1",
            ]);
        if($list!==false) {
            $this->success('删除成功');
        }else{   
            $this->error( '删除失败');
        } 

    }
}